/****************************************************************
 *           滚动轮播
 *
 * 参数：data 和 opt
 * opt { derict : 'top/left'; time : 3000/?? }
 *
*/
$.fn.slideScroll = function(data,opt){
	//参数设置
	var option  = { driect : 'left', time : 3000 };
	opt = opt || option ;
	var argSet  = {};
	argSet.top  = opt.driect == 'top' ? true : false;
	argSet.left = opt.driect == 'left' ? true : false;

	var _this   = this;
	var temRnd  = Math.random().toString();
	var rnd     = temRnd.substr(2,temRnd.length-2);
	var frame   = '<div id="box_' + rnd + '"><ul id="ul_' + rnd + '"></ul><span id="num_' + rnd + '"></span><div id="title_' + rnd + '"></div><div id="title_bg' + rnd + '"></div></div>'; //组织html基础结构
	var frame_inner = frame_serial = frame_title = '';
	var w = _this.width(),h = _this.height();

	//初始化结构
	_this.append(frame);
	var _box     = $('#box_' + rnd);
	var _ul      = $('#ul_' + rnd);
	var _num     = $('#num_' + rnd);
	var _title   = $('#title_' + rnd);
	var _titleBg = $('#title_bg' + rnd);
	_box.css({
		width     : w,
		height    : h,
		position  : 'relative',
		overflow  : 'hidden'
	});
	_ul.css({
		width     : argSet.top ? w : data.length * w,
		height    : argSet.top ? data.length * h : h,
		position  : 'absolute'
	});
	_num.css({
		position  : 'absolute',
		right     : '0',
		bottom    : '0px',
        zIndex    : '1000'
	});
	_title.css({
		position  : 'absolute',
		bottom    : '3px',
		color     : '#fff',
		width     : '100%',
		zIndex    : 5,
		textAlign : 'center'
	});
	_titleBg.css({
		position  : 'absolute',
		bottom    : '0',
		background : '#000',
		opacity   : '.3',
		filter    : 'alpha(opacity=30)',
		color     : '#fff',
		width     : '100%',
		height    : 23,
		zIndex    : 4
	});

	var styleLi = argSet.left ? 'float:left' : ''; //如果设置
	for (var i = 0; i < data.length ; i++ ) {
		//图片结构
		frame_inner  += '<li style="width:' + w + 'px; height:' + h + 'px;' + styleLi + ';">';
		frame_inner  +=	'<a href="' + data[i].url + '" title="' + data[i].title + '" target="_blank">';
		frame_inner  +=		'<img src="' + data[i].src + '" alt="' + data[i].title + '" width="' + w + '" height="' + h + '" />';
		frame_inner  +=	'</a>';
		frame_inner  += '</li>';

		//序号结构
		frame_serial += '<span style="display:inline-block; zoom:1; z-index:1000;width:12px; height:12px; line-height:14px; text-align:center; border:1px solid #ff6600; background:#ffcda4; color:#333; margin:0 10px 0 0; font-size:12px; opacity:.6; filter:alpha(opacity=60);vertiacl-align:bottom; cursor:pointer">';
		frame_serial +=		i + 1;
		frame_serial += '</span>';

		//标题结构
		frame_title += '<a style="color:#fff;text-decoration:none;' + (i === 0 ? 'display:none;"' : 'display:block;') + '" href="' + data[i].url + '" title="' + data[i].title + '" target="_blank">' + data[i].title + '</a>';
	};

	_ul.append(frame_inner);
	_num.append(frame_serial);
	_title.append(frame_title);

	function scroll(tPos) {
		if (argSet.left) {
			_ul.animate({
				left : tPos
			},500)
		} else {
			_ul.animate({
				top : tPos
			},500)
		}
	}

	function signCurr (num) {
		_num.find('span').eq(num).css({
			background : '#f7994d',
			padding    : '4px',
			color      : '#fff',
			opacity    : '1',
			filter     : 'alpha(opacity=100)',
			fontWeight : '800'
		}).siblings().css({
			background : '#ffcda4',
			padding    : '2px',
			color      : '#333',
			opacity    : '.5',
			filter     : 'alpha(opacity=50)',
			fontWeight : '200'
		});
		_title.find('a').eq(num).show().siblings().hide();
	};

	var timer,c_num = 0;
	function start() { //current position || target position
		signCurr(c_num);
		if (argSet.top) { //上下滚动
			timer = setInterval(function(){
				if (Math.abs(parseInt(_ul.css('top'))) < h*(data.length-1)) {
					signCurr(c_num+1);
					scroll(-h*(c_num+1));
					c_num += 1;
				} else {
					c_num = 0;
					signCurr(c_num);
					scroll(0);
				}
			},opt.time)
		};
		if (argSet.left) { //左右滚动
			signCurr(c_num);
			timer = setInterval(function(){
				if (Math.abs(parseInt(_ul.css('left'))) < w*(data.length-1)) {
					signCurr(c_num+1);
					scroll(-w*(c_num+1));
					c_num += 1;
				} else {
					c_num = 0;
					signCurr(0);
					scroll(0);
				}
			},opt.time)
		}
	}

	function stp() { clearTimeout(timer); }

	(function() { //run
			start();
			//鼠标滚动
			_num.find('span').each(function(){
				$(this).mouseover(function(){
					_ul.stop();
					stp();
					var curN = parseInt($(this).text());
					c_num = curN-1;
					signCurr(c_num);
					var o = argSet.left ? w : h;
					scroll(-o*(c_num));
				}).mouseout(function(){
					start();
				})
			});

			_ul.find('li').each(function(){
				$(this).mouseover(function(){
					stp();
				}).mouseout(function(){
					start();
				})
			});
			_title.find('a').each(function(){
				$(this).mouseover(function(){
					stp();
				}).mouseout(function(){
					start();
				})
			});
	})()

};

$.fn.scroll = function(data,opt){ // by Even[ websev Team ]
	var $t = $(this),
		opt = opt || {};

	var main = (function(){
		return {
			// 对象空间
			el : {},

			// 默认配置
			opt : {
				time : 3000,                        // 时间
				act : 'fade',                       // 切换效果
				event : ['mouseover','mouseout']    // 触发事件
			},

			// 结构
			frame : function() {
				var html = '';
				html += '<div id="plug_scroll">';
				html += '	<div id="p_s_img1"><ul></ul></div>';
				html += '	<div id="p_s_tit"><ul></ul></div>';
				html += '	<div id="p_s_img2"><ul></ul></div>';
				html += '	<div id="p_s_num"></div>';
				html += '</div>';
				$t.html(html);

				this.el.B   = $('#plug_scroll');
				this.el.I1  = $('#p_s_img1');
				this.el.T   = $('#p_s_tit');
				this.el.I2  = $('#p_s_img2');
				this.el.N   = $('#p_s_num');
				this.el._I1 = $('#p_s_img1 ul');
				this.el._T  = $('#p_s_tit ul');
				this.el._I2 = $('#p_s_img2 ul');
			},

			// 函数集
			fn : {
				random : function(){return new Date().getTime()}
			},

			// 切换动作
			go : function(){
					var curr = this.curr;
					this.el.Is2.eq(curr).addClass('curr').siblings().removeClass('curr');
					this.el.Ns.eq(curr).addClass('curr').siblings().removeClass('curr');
					this.el.Ts.eq(curr).addClass('curr').show().siblings().removeClass('curr').hide();
					switch (this.opt.act){
						case 'scrollLeft': // 左右滚动
							this.el._I1.stop().animate({
								left : -curr * this.opt.oneW
							},500);
							break;
						case 'scrollTop': // 上下滚动
							this.el._I1.stop().animate({
								top : -curr * this.opt.oneH
							},500);
							break;
						default :
							this.el.Is1.eq(curr).fadeIn(150).siblings().hide();
							break;
					};

			},

			// 装载数据
			load : function(){
				var d = this.D,
					h_img = '',
					h_tit = ''
					h_num = '';
				for (var i = 0; i < d.length ; i++ ){
					d[i].cont = d[i].cont == undefined ? '' : d[i].cont; // 不传cont，则为空
					h_img += '<li><a href="' + d[i].url + '" title="' + d[i].title + '" target="_blank"><img src="' + d[i].src + '" alt="' + d[i].title + '" /></a></li>';
					h_tit += '<li><a href="' + d[i].url + '" title="' + d[i].title + '" target="_blank">' + d[i].title + '</a><p>' + d[i].cont + '</p></li>';
					h_num += '<em>' + (i + 1) + '</em>';
				};
				this.el._I1.html(h_img);
				this.el._I2.html(h_img);
				this.el.N.html(h_num);
				this.el._T.html(h_tit);

				this.el.Is1 = this.el._I1.find('li');
				this.el.Is2 = this.el._I2.find('li');
				this.el.Ns  = this.el.N.find('em');
				this.el.Ts  = this.el._T.find('li');

				this.opt.oneW = this.el.Is1.eq(0).outerWidth();
				this.opt.oneH = this.el.Is1.eq(0).outerHeight();

				this.el._I1.css({
					width : this.opt.oneW * d.length,
					height : this.opt.oneH * d.length
				});

				this.el.Is2.css({
					width : this.opt.oneW / d.length
				});


				if(this.opt.act == 'scrollLeft'){
					this.el.Is1.css({
						float : 'left'
					});
				}
			},

			// 初始化
			init : function(){
				this.D = data;
				this.curr = 0;
				for (var i in this.opt){ // 加载配置
					this.opt[i] = opt[i] === undefined ? this.opt[i] : opt[i];
				}
				this.frame();
				this.load();
				this.go();
			},

			// 自动播放
			start : function(){
				var _this = this;
				this.timer = setInterval(function(){
					_this.curr += 1;
					_this.curr = _this.curr%(_this.D.length);
					_this.go();
				},this.opt.time)
			},

			// 暂停
			stop  : function(){
				clearInterval(this.timer);
			},

			// 运行
			run : function(){
				var _this = this;
				this.init();
				//this.start();
				this.el.Is2.each(function(i){  // 缩略图
					$(this).bind(_this.opt.event[0],function(){
						_this.curr = i;
						_this.go(i);
						_this.stop();
					}).bind(_this.opt.event[1],function(){
						_this.start();
					});
				});

				this.el.Ns.each(function(i){ // 当前项数字
					$(this).bind(_this.opt.event[0],function(){
						_this.curr = i;
						_this.go(i);
						_this.stop();
					}).bind(_this.opt.event[1],function(){
						_this.start();
					});
				});
			}
		}
	})();
	main.run();
}